var listColumns = [[
    {field: "id", checkbox: true},
    {field: "qywxId", title: "企业微信id", align: "center", width: 140},
    {field: "deptName", title: "部门名称", align: "center", width: 100},
    {field: "description", title: "描述", align: "center", width: 120},
    {field: "enabled", title: "是否可用", align: "center", width: 120,formatter: function (value,rowData,index) {
        switch (value){
            case true: return "可用";
            case false: return "不可用";
        }
    }},
    {field: "ownerSystem", title: "归属系统", align: "center", width: 120,formatter: function (value) {
        if(value == 'QYWX'){
            return "企业微信";
        }
        return value;
    }},
    {field: "createTime", title: "创建时间", align: "center", width: 140}

]];

layui.config({
    base: "../static/agent-ui/js/"
});

$.fn.toJSON = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        var name = this.name;
        var value = this.value;
        var paths = this.name.split(".");
        var len = paths.length;
        var obj = o;
        $.each(paths, function (i, e) {
            if (i == len - 1) {
                if (obj[e]) {
                    if (!obj[e].push) {
                        obj[e] = [obj[e]];
                    }
                    obj[e].push(value || '');
                } else {
                    obj[e] = value || '';
                }
            } else {
                if (!obj[e]) {
                    obj[e] = {};
                }
            }
            obj = o[e];
        });
    });
    return o;
};

layui.use(["laypage", "layer", "validator", "tools", "form", "select", "laydate"], function () {
    var form = layui.form;
    var date = layui.laydate;
    date.render({
        elem: '#ringTime',
        type: 'datetime',
        range: true
    });


    var layer=layui.layer;

    $(function () {
        $('#list').datagrid({
            columns: listColumns,
            fitColumns: false,
            fit: true,
            singleSelect: true,
            pagination: true,
            pageSize: 30,
            pageList: [30, 50, 100, 1000],
            toolbar: '#receivedListHeader',
            method: 'get',
            url: ctx +'/dept/list',
            onLoadSuccess: function () {


            }
        });
    });

    $('#search_submit').on('click', function () {
        $('#list').datagrid('load', $('#search_form').toJSON());
    });

    $("#syncDept").on("click",function(){
        layer.confirm("是否同步", {
            btn: ['是','否'] //按钮
        }, function(index){
            $.ajax({
                url:ctx+'/schedule/dept',
                type : 'GET',
                dataType: 'json',
                success:function (result) {
                   if(result.code =='0'){
                       layer.close(index);
                       $('#list').datagrid('reload');
                   }else{
                       layer.open({
                           title: '报错了'
                           ,content: result.msg
                       });
                   }
                },
                error:function (result) {
                    layer.open({
                        title: '报错了'
                        ,content: '报错了 请联系管理员'
                    });
                }
            });
        },function(index){
            layer.close(index);
        });
    });

});





